﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TV4Home.Server.TVEInteractionLibrary;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;
using TV4Home.WebInterface.UI.Code;

namespace TV4Home.WebInterface.UI
{
    public partial class ProgramInfoDialog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
                UpdateUI();
        }

        private void UpdateUI()
        {
            try
            {
                LoadCorrectStyleSheet();
                ShowProgramInfo();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;

                this.pnContent.Visible = false;
                this.pnError.Visible = true;
            }
        }

        private void LoadCorrectStyleSheet()
        {
            bool embedded = false;

            if (!String.IsNullOrEmpty(Request["embedded"]))
                embedded = Convert.ToBoolean(Request["embedded"]);

            if (embedded)
                this.ProgramInfoDialogStyleSheet.Attributes.Add("href", "style/ProgramInfoDialog_embedded.css");
            else
                this.ProgramInfoDialogStyleSheet.Attributes.Add("href", "style/ProgramInfoDialog_standalone.css");
        }

        void ShowProgramInfo()
        {
            if (String.IsNullOrEmpty(Request["id"]))
                throw new ArgumentException("No program ID specified!");

            int id = Int32.Parse(Request["id"]);

            if (id == 0)
                throw new ArgumentException("The specified ID is invalid!");

            WebProgramDetailed program = WebService.TV4HomeWebService.ServiceInstance.GetProgramDetailedById(id);
            lblChannel.Text = WebService.TV4HomeWebService.ServiceInstance.GetChannelDetailedById(program.IdChannel).DisplayName;
            lblTitle.Text = program.Title;
            lblDescription.Text = program.Description.Replace("\n", "<br/>");
            lblGenre.Text = program.Genre;
            lblStart.Text = program.StartTime.ToShortTimeString();
            lblEnd.Text = program.EndTime.ToShortTimeString() + "  " + program.StartTime.ToLongDateString();
            lblIsRecording.Text = Utils.GetYesNoFromBoolean(program.IsScheduled);

            mvRecordOrCancel.ActiveViewIndex = program.IsScheduled ? 1 : 0;
        }

        protected void btnRecord_Click(object sender, EventArgs e)
        {
            try
            {
                RecordProgram();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;

                this.pnContent.Visible = false;
                this.pnError.Visible = true;
            }

            UpdateUI();
        }

        protected void btnCancelRecording_Click(object sender, EventArgs e)
        {
            try
            {
                CancelRecording();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;

                this.pnContent.Visible = false;
                this.pnError.Visible = true;
            }

            UpdateUI();
        }

        private void RecordProgram()
        {
            int id = Int32.Parse(Request["id"]);
            WebProgramBasic program = WebService.TV4HomeWebService.ServiceInstance.GetProgramBasicById(id);

            int scheduleType = Convert.ToInt32(this.ddlRecordScheduleType.SelectedValue);

            if (!program.IsScheduled)
            {
                WebService.TV4HomeWebService.ServiceInstance.AddSchedule(program.IdChannel, program.Title, program.StartTime, program.EndTime, scheduleType);
            }
        }

        private void CancelRecording()
        {
            int programId = Int32.Parse(Request["id"]);

            WebService.TV4HomeWebService.ServiceInstance.CancelSchedule(programId);
        }
    }
}